package com.lzh.others.basejava.Thread_21;

import java.util.concurrent.TimeUnit;

/**
 * @Classname SimpleDaemons
 * @Description 后台线程
 * @Date 2021/9/14 11:58
 * @Created by liuzihao
 */
public class SimpleDaemons implements Runnable {

    @Override
    public void run() {
//        try {
//            while (true) {
//                TimeUnit.MILLISECONDS.sleep(100);
//                System.out.println(Thread.currentThread() + " " + this);
//            }
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//            System.out.println("sleep() interrupted");
//        }
        System.out.println(Thread.currentThread() + " " + this);

    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            Thread daemon = new Thread(new SimpleDaemons());
            System.out.println("daemon = " + daemon.getState());
            daemon.setDaemon(true);
            daemon.start();
            System.out.println("daemon = " + daemon.getState());
            TimeUnit.MILLISECONDS.sleep(1000);
            System.out.println("daemon = " + daemon.getState());
        }
        System.out.println("All daemons started");
        TimeUnit.MILLISECONDS.sleep(1000);
    }
}
